package com.tairanchina.md.meeting.application;

import com.tairanchina.md.elife.rabbit.client.consumer.RabbitConsumerClient;
import com.tairanchina.md.elife.rabbit.client.consumer.impl.TXConsumerRabbitClient;

import com.tairanchina.md.elife.rabbit.client.producer.RabbitProducerClient;
import com.tairanchina.md.elife.rabbit.client.producer.impl.TXProducerRabbitClient;
import com.tairanchina.md.elife.rabbit.constants.ConnConstants;
import com.tairanchina.md.elife.rabbit.constants.ConnParamConstants;
import com.tairanchina.md.meeting.constants.MeetingConstants;
import com.tairanchina.md.meeting.constants.RabbitConnConstants;
import com.tairanchina.md.meeting.rabbit.SignTask;
import com.tairanchina.md.meeting.resource.PushProgramTipWS;
import com.tairanchina.md.meeting.resource.PushSunAwarderWS;

import com.tairanchina.md.meeting.rabbit.ProgramLikeTask;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;


import javax.ws.rs.ApplicationPath;

/**
 * @author liuxuan
 * @time 2016年10月21日
 */
@ApplicationPath("/rest/*")
public class MeetingApplication extends ResourceConfig
{
    public MeetingApplication()
    {
        packages("com.tairanchina.md.meeting.resource");
        packages("com.tairanchina.md.meeting.filter");

        property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
        property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true);
        initRabbitMQ();

        programLikeRun();
        programTipRun();
        sunAwarderRun();
/*        programSignRun();*/
/*        System.out.println(MeetingConstants.ENDTIME);*/
        signRun();

    }


    private void programLikeRun() {
        ProgramLikeTask likeTask = new ProgramLikeTask();
        Thread likeThread = new Thread(likeTask);
        likeThread.start();
    }

    /**
     * 阳光普照摇奖推送
     */
    private void sunAwarderRun() {
        PushSunAwarderWS awarderWS = new PushSunAwarderWS();
        Thread likeThread = new Thread(awarderWS);
        likeThread.start();
    }
    /**
     * 节目打赏队列
     */
    private void programTipRun() {
        PushProgramTipWS tipWS = new PushProgramTipWS();
        Thread likeThread = new Thread(tipWS);
        likeThread.start();
    }

    /**
     * 用户签到消费者
     */
    private  void signRun(){
        SignTask signTask = new SignTask();
        Thread signThread = new Thread(signTask);
        signThread.start();
    }
    /**
     * 初始化rabbitMQ
     */
    private void initRabbitMQ() {
        initProducerClient();
        initConsumerClient();
    }

    /**
     * 初始化生产者客户端
     */
    private void initProducerClient(){
        RabbitProducerClient rabbitClient = new TXProducerRabbitClient();

        //初始化发送客户端
/*
        rabbitClient.setHost("120.27.192.60");
        rabbitClient.setPort(5672);
        rabbitClient.setVirtualhost("/");
        rabbitClient.setUsername("mdtest");
        rabbitClient.setPassword("mdtest");
*/

        //初始化发送客户端  开发服
      /*  rabbitClient.setHost("172.30.248.197");
        rabbitClient.setPort(5672);
        rabbitClient.setVirtualhost("/");
        rabbitClient.setUsername("root");
        rabbitClient.setPassword("root");*/

        rabbitClient.setHost(RabbitConnConstants.host);
        rabbitClient.setPort(5672);
        rabbitClient.setVirtualhost("/");
        rabbitClient.setUsername(RabbitConnConstants.userName);
        rabbitClient.setPassword(RabbitConnConstants.password);

        rabbitClient.init();
        initSunStateQueue(rabbitClient);
        initLotteryQueue(rabbitClient);
       ConnConstants.setProducerClient(rabbitClient);
    }


    /**
     * 初始化消费者客户端
     */
    private void initConsumerClient(){
        RabbitConsumerClient rabbitClient = new TXConsumerRabbitClient();

        //初始化发送客户端
   /*     rabbitClient.setHost("120.27.192.60");
        rabbitClient.setPort(5672);
        rabbitClient.setVirtualhost("/");
        rabbitClient.setUsername("mdtest");
        rabbitClient.setPassword("mdtest");*/

        //初始化发送客户端
     /*   rabbitClient.setHost("172.30.248.197");
        rabbitClient.setPort(5672);
        rabbitClient.setVirtualhost("/");
        rabbitClient.setUsername("root");
        rabbitClient.setPassword("root");*/

        rabbitClient.setHost(RabbitConnConstants.host);
        rabbitClient.setPort(5672);
        rabbitClient.setVirtualhost("/");
        rabbitClient.setUsername(RabbitConnConstants.userName);
        rabbitClient.setPassword(RabbitConnConstants.password);

        rabbitClient.init();
        initSignQueue(rabbitClient);
        initSunAwarderQueue(rabbitClient);
        initTipQueue(rabbitClient);
        initVoteQueue(rabbitClient);
        initSunStateLocal(rabbitClient);
        ConnConstants.setConsumerClient(rabbitClient);
    }

    /**
     * 初始化用户签到队列
     * @param rabbitClient
     */
    private void initSignQueue(RabbitConsumerClient rabbitClient){

        //初始化路由，并绑定对应的队列
        String exchangeName = MeetingConstants.EXCHANGE_NAME_YEAR_WATER;
        String[] queueNames = {MeetingConstants.QUEUE_NAME_YEAR_WATER_LOCAL};
        String[] queueKeys = {MeetingConstants.QUEUE_KEY_YEAR_WATER};
        rabbitClient.initExchange(exchangeName, ConnParamConstants.DIRECT, queueNames, queueKeys);
    }

    /**
     * 初始化设置阳光普照抽奖状态队列
     * @param producerClient
     */

    private void initSunStateQueue(RabbitProducerClient producerClient){
        //初始化路由，并绑定对应的队列
        String exchangeName = MeetingConstants.EXCHANGE_NAME_SUN_STATE;
        String[] queueNames = {MeetingConstants.QUEUE_NAME_SUN_STATE};
        String[] queueKeys = {MeetingConstants.QUEUE_KEY_SUN_STATE};
        producerClient.initExchange(exchangeName, ConnParamConstants.DIRECT, queueNames, queueKeys);

    }
    /**
     * 初始化本地抽奖队列
     * @param producerClient
     */

    private void initLotteryQueue(RabbitProducerClient producerClient){
        //初始化路由，并绑定对应的队列
        String exchangeName = MeetingConstants.EXCHANGE_NAME_YEAR_LOTTERY;
        String[] queueNames = {MeetingConstants.QUEUE_NAME_YEAR_LOTTERY};
        String[] queueKeys = {MeetingConstants.QUEUE_KEY_YEAR_LOTTERY};
        producerClient.initExchange(exchangeName, ConnParamConstants.DIRECT, queueNames, queueKeys);

    }

    /**
     * 初始化节目打赏队列
     * @param rabbitClient
     */
    private void initTipQueue(RabbitConsumerClient rabbitClient){
        //初始化路由，并绑定对应的队列
        String exchangeName = MeetingConstants.EXCHANGE_NAME_YEAR_REWARD;
        String[] queueNames = { MeetingConstants.QUEUE_NAME_YEAR_REWARD};
        String[] queueKeys = {MeetingConstants.QUEUE_KEY_YEAR_REWARD};
        rabbitClient.initExchange(exchangeName, ConnParamConstants.DIRECT, queueNames, queueKeys);

    }

    /**
     * 初始化节目点赞队列
     * @param rabbitClient
     */
    private void initVoteQueue(RabbitConsumerClient rabbitClient){

        //初始化路由，并绑定对应的队列
        String exchangeName = MeetingConstants.EXCHANGE_NAME_YEAR_VOTE;
        String[] queueNames = {MeetingConstants.QUEUE_NAME_YEAR_VOTE};
        String[] queueKeys = {MeetingConstants.QUEUE_KEY_YEAR_VOTE};
        rabbitClient.initExchange(exchangeName, ConnParamConstants.DIRECT, queueNames, queueKeys);

    }

    /**
     * 初始化阳光普照抽奖队列
     * @param rabbitClient
     */
    private void initSunAwarderQueue(RabbitConsumerClient rabbitClient){

        //初始化路由，并绑定对应的队列
        String exchangeName = MeetingConstants.EXCHANGE_NAME_SUN;
        String[] queueNames = {MeetingConstants.QUEUE_NAME_SUN_LOCAL};
        String[] queueKeys = {MeetingConstants.QUEUE_KEY_YEAR_SUN_LOCAL};
        rabbitClient.initExchange(exchangeName, ConnParamConstants.DIRECT, queueNames, queueKeys);

    }

    /**
     * 阿里云响应阳光普照开光状态
     * @param rabbitClient
     */
    private void initSunStateLocal(RabbitConsumerClient rabbitClient){

        //初始化路由，并绑定对应的队列
        String exchangeName = MeetingConstants.EXCHANGE_NAME_SUN_STATE_LOCAL;
        String[] queueNames = {MeetingConstants.QUEUE_NAME_SUN_STATE_LOCAL};
        String[] queueKeys = {MeetingConstants.QUEUE_KEY_SUN_STATE_LOCAL};
        rabbitClient.initExchange(exchangeName, ConnParamConstants.DIRECT, queueNames, queueKeys);

    }

}

